package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.BarcodeScanner;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.ScanBarcodeAdapter;
import com.rfid.emergencywarehouse.adapter.ShelfingAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.manager.FragmentDataManager;
import com.rfid.emergencywarehouse.presenter.ShelfingPresenter;
import com.rfid.emergencywarehouse.presenter.ShelfingWithOrderPresenter;
import com.rfid.emergencywarehouse.view.IShelfingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public abstract class ShelfingFragment<T extends ShelfingPresenter> extends BaseScanListFragment<ShelfingAdapter, BarcodeInfo, T>implements FragmentDataManager.Listener, CompoundButton.OnCheckedChangeListener, IShelfingView, BarcodeScanner.Listener {

    @BindView(R.id.scan)
    CheckBox mRfid;

    @BindView(R.id.scan_kw)
    CheckBox mKWRfid;

    @BindView(R.id.barcode)
    EditText mBarcodeEdit;

    @BindView(R.id.kwbarcode)
    EditText mKWBarcodeEdit;

    BarcodeScanner mScanner;

//    @Override
//    protected String getScanTip(int size) {
//        return String.format("已扫描数据%s条，请选择你要上架的物资",size);
//    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
        mKWRfid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPresenter.setDisableShelf(false);
                if (isChecked) {
                    mPresenter.startScanRfid();
                    mRfid.setEnabled(false);
                } else {
                    mPresenter.stopScanRfid();
                    mRfid.setEnabled(true);
                }
            }
        });
    }

    @Override
    protected boolean needSelect(BarcodeInfo info) {
        return !info.isNew();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mScanner = new BarcodeScanner(context,this);
        mScanner.listenBarcode(context);
    }

    @Override
    public void onDetach() {
        mScanner.releaseListenBarcode(getContext());
        super.onDetach();
    }

    @Override
    protected int getScanRfidCheckBoxId() {
        return R.id.scan;
    }

    @Override
    protected EditText getBarcodeEdit() {
        return mBarcodeEdit;
    }

    @Override
    protected ShelfingAdapter getAdapter() {
        return new ShelfingAdapter(mData);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_shelfing;
    }

    @OnClick(R.id.add)
    public void add(View v){
        String barcode = mBarcodeEdit.getText().toString();
        String shelf = mKWBarcodeEdit.getText().toString();
//        if (TextUtils.isEmpty(barcode)) {
//            AnimatorHelper.shake(getContext(),mBarcodeEdit);
//            return;
//        }
        if (TextUtils.isEmpty(shelf) && mSelects.size() == 0) {
            AnimatorHelper.shake(getContext(),mKWBarcodeEdit);
            return;
        }
        List<String> barcodes = getBarcodes();
        if (barcodes == null) {
            return;
        }
        mPresenter.shelfing(shelf,barcodes);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mPresenter.setDisableShelf(true);
        super.onCheckedChanged(buttonView,isChecked);
        if (isChecked) {
            mKWRfid.setEnabled(false);
        } else {
            mKWRfid.setEnabled(true);
        }
    }

    private void showScanStatus(boolean isChecked) {
        System.out.println("showScanStatus isChecked = " + isChecked);
        if (isChecked) {
            mBarcodeEdit.setEnabled(false);
            mBarcodeEdit.setText("正在扫描中");
            mBarcodeEdit.setHint("正在扫描中");
            mBarcodeEdit.setTextColor(Color.parseColor("#409EFE"));
//            mKWBarcodeEdit.setEnabled(false);
//            mKWBarcodeEdit.setText("正在扫描中");
//            mKWBarcodeEdit.setTextColor(Color.parseColor("#409EFE"));
        } else {
            mBarcodeEdit.setText("");
            mBarcodeEdit.setEnabled(true);
            mBarcodeEdit.setTextColor(Color.parseColor("#666666"));
            mBarcodeEdit.setHint("请输入/扫描编码");
//            mKWBarcodeEdit.setText("");
//            mKWBarcodeEdit.setEnabled(true);
//            mKWBarcodeEdit.setTextColor(Color.parseColor("#666666"));
        }
    }

    @Override
    public void onKuwei(String shelf) {
    }

    @Override
    public void onScanStatus(boolean scan) {
        if (!scan) {
            mKWRfid.setChecked(false);
            mRfid.setChecked(false);
        }
        showScanStatus(scan);
    }

    @Override
    public void onShelfingResult(boolean succes, String msg) {
        if (!succes) {
//            Context context = getContext();
//            if (context != null) {
//                Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
//            }
            showTip(msg);
        } else {
            showSuccess("上架成功");
            mBarcodeEdit.setText("");
            mKWBarcodeEdit.setText("");
            clearSelectData();
        }
    }

    @Override
    protected boolean needAdd(BarcodeInfo info) {
        return !info.isNew();
    }

    @Override
    public void onScanBarcode(String barcode) {
        mKWBarcodeEdit.setText(barcode);
    }
}
